<?php
use \PsiFramework\Classes\Util\UI\Printer;
use \PsiFramework\Classes\Util\UI\HTML;
use \PsiFramework\Classes\Util\Log\Logger;

$objectType = $response->getAttribute("objectType");

$list = $response->getAttribute("list");
$columns = $response->getAttribute("columns");

$orderColumn = $response->getAttribute("orderColumn");
$sens = $response->getAttribute("sens");
$moreParams = $response->getAttribute("moreParams");

$page = $response->getAttribute("page");
$nbResults = $response->getAttribute("nbResults");
$totalResults = $response->getAttribute("totalResults");

$searchService = $response->getAttribute("searchService");

$logger = new Logger("List.php");

$newParams = "" ;
$typeId = $response->getAttribute("typeId");
if ($typeId!=null) $newParams = "&id_type=" . $typeId ;
?>
<p>
    <?php echo HTML::link("Index"); ?>
    -
    <?php
    echo HTML::link(
        "Nouveau",
        "action=CMS.Edit&type=" . $objectType . $newParams
    );
    ?>
</p>
<?php 
if (!isNullOrEmptyString($searchService)) {
?>
<p>
    Search : <input type="text" name="search" class="searchable" />
    <script type="text/javascript">
        $(document).ready(function() {
            $(".searchable").autocomplete({
                source: "?service=<?php echo $searchService; ?>&format=json",
                select: function(event, ui) {
                    var id = ui.item.id;
                    var url = "?action=CMS.Edit&type=<?php echo $objectType; ?>&id="+id;
                    location.href = url;
                }
            });
        });
    </script>
</p>
<?php
}
?>
<table class="list">
    <thead>
        <tr class="listTitles">
            <?php 

            $mp = "" ;
            if ($moreParams != null) {
                foreach ($moreParams as $nomParam => $valueParam) {
                    $mp .= "&amp;" . $nomParam . "=" . $valueParam ;
                }
            }

            foreach ($columns as $name=>$sqlColumn) {

                if ($sqlColumn->display) {

                    $cs = "ASC" ;
                    if ($orderColumn == $sqlColumn->name) {
                        if ($sens == "ASC") {
                            $cs = "DESC" ;
                        } else {
                            $cs = "ASC" ;
                        }
                    }
            ?>
            <th>
                <?php
                if (
                    $sqlColumn->isPrimitive() ||
                    $sqlColumn->isPrimitiveObject()
                ) {
                    echo HTML::link(
                        $sqlColumn->name,
                        "action=CMS.List&type=".$objectType.
                        "&orderColumn=".$sqlColumn->name.
                        "&sens=".$cs."&page=".$page.
                        "&nbResults=".$nbResults . $mp
                    );
                } else {
                    echo $sqlColumn->name ;
                }
                ?>
            </th>
            <?php
                }
            }
            ?>
            <th class="listActions"></th>
            <th class="listActions"></th>
            <th class="listActions"></th>
        </tr>
    </thead>
    <tbody>
        <?php
        $i = 1 ;

        foreach ($list as $object) {

            if (!is_null($object)) {

                $className = "evenRow" ;
                if ($i%2==1) $className = "oddRow" ;
        ?>
        <tr class="<?php Printer::display($className); ?>">
        <?php
                foreach ($columns as $name=>$sqlColumn) {
                    if ($sqlColumn->display) {
                        $method = $sqlColumn->getter ;
                        $element = $object->$method();
                        $string = "" ;
                        if (!is_null($element)) {
                            if (is_array($element)) {
                                foreach ($element as $item) {
                                    if ($string != "") $string .= "\n" ;
                                    if (is_string($item)) $string .= $item;
                                    else $string .= $item->toString();
                                }
                            } else {
                                if (
                                    $sqlColumn->type == "bool" ||
                                    $sqlColumn->type == "boolean"
                                ) {
                                    if ($element) $string = "true" ;
                                    else $string = "false" ;
                                } else if ($sqlColumn->isPrimitive()) {

                                    $string = $element;
                                }
                                else $string = $element->toString();
                            }
                        }
                        echo "    <td>" ;
                        Printer::display($string);
                        echo "</td>\n" ;
                    }
                }
                
                $linkEnd = '&amp;type='.$objectType.'&amp;id='.$object->getId();
                $showLink   = HTML::linkHref('action=CMS.Show'.$linkEnd);
                $editLink   = HTML::linkHref('action=CMS.Edit'.$linkEnd);
                $deleteLink = HTML::linkHref('action=CMS.Delete'.$linkEnd);
        ?>
            <td class="showObject">
                <a href="javascript:window.location='<?php echo $showLink; ?>';">
                    Voir
                </a>
            </td>
            <td class="editObject">
                <a href="javascript:window.location='<?php echo $editLink; ?>';">
                    Modifier
                </a>
            </td>
            <td class="deleteObject">
                <a href="javascript:if (
                        confirm('Etes-vous sûr de vouloir supprimer l\'entrée ?')
                    ) {
                        window.location='<?php echo $deleteLink; ?>';
                    }"
                >
                    Supprimer
                </a>
            </td>
        </tr>
        <?php
                $i++;
            }
        }
        ?>
    </tbody>
</table>
<?php if (!isNullOrEmptyString($totalResults) && $totalResults>$nbResults) { ?>
<nav>
    <?php 
    $totalPages = ceil($totalResults/$nbResults);
    for ($i=1; $i<=$totalPages;$i++) {
        if ($i!=$page) {
            echo HTML::link(
                $i,
                "action=CMS.List&type=".$objectType.
                "&orderColumn=".$orderColumn.
                "&sens=".$sens.
                "&page=".$i.
                "&nbResults=".$nbResults . $mp
            ) . "\n" ;
        } else {
            echo $i . "\n" ;
        }
    }
    ?>
</nav>
<?php
} 
